package data;

import tool.PixelTool;
import h2d.Tile;
import haxe.io.Bytes;
import hxd.Pixels;
import haxe.io.Int32Array;

class InnerData_Canvas {
    public var width(default, null) : Int;
    public var height(default, null) : Int;
    public var curLayer(default, null) : Int;
    //层级遮罩
    public var layerMask : Int;
    var layersNum : Int;
    var colorsList : Array<Array<Int>>;
    var depths : Array<Int>;
    var baseColor : Int;
    public function new() {
        this.baseColor = 0xffffff;
        this.width = 16;
        this.height = 16;
        this.layersNum = 1;
        this.layerMask = (1 << 32) - 1;
        this.curLayer = 0;
        this.colorsList = new Array<Array<Int>>();
        this.colorsList.resize(layersNum);
        var ary = new Array<Int>();
        ary.resize(width * height);
        colorsList[0] = ary;
        var start = 0;
        var end = width * height;
        for (i in start ... end){
            this.colorsList[curLayer][i] = baseColor;
        }
        this.depths = new Array<Int>();
        this.depths.resize(width * height);
    }
    
    //设置新颜色  返回旧颜色
    public function ChangeColor(x:Int, y:Int, layer:Int, color:Int) {
        var id = ConvertID(x, y);
        var ocolor = colorsList[layer][id];
        colorsList[layer][id] = color;
        return ocolor;
    }

    //获取颜色
    public function GetColor(x:Int, y:Int, layer:Int) {
        var id = ConvertID(x, y);
        var color = colorsList[layer][id];
        return color;
    }

    //设置颜色
    public function SetColor(x:Int, y:Int, layer:Int, color:Int) {
        var id = ConvertID(x, y);
        colorsList[layer][id] = color;
    }
    //转换ID
    function ConvertID(x, y) {
        return y * width + x;
    }

    //设置深度图
    public function SetDepths() {
		for (i in 0...height * width) {
            depths[i] = -1;
			for (n in 0...layersNum) {
				var layer = layersNum - 1 - n;
                if(IsOpen(layer)){
                    var color = colorsList[layer][i];
                    if(color != baseColor) depths[i] = layer;
                }
			}
		}
    }

    //层级是否打开
    public function IsOpen(layer) {
        return layerMask & (1 << layer) != 0;
    }

    public function GetPixels() {
        return PixelTool.ColorsToPixels(GetColors(), width, height);
    }

    public function GetPixelsByLayer(layer) {
        return PixelTool.ColorsToPixels(colorsList[layer], width, height);
    }

    public function GetLayerColors(layer) {
        return colorsList[layer];
    }

    public function SetLayerColors(layer, colors) {
        colorsList[layer] = colors;
    }
    
    public function ChangeLayerColors(layer, colors) {
        var ocolors = colorsList[layer];
        colorsList[layer] = colors;
        return ocolors;
    }

    public function GetColors() {
        SetDepths();
        var pixels = new Array<Int>();
        pixels.resize(width * height);
        for (i in 0 ... width * height){
            var depth = depths[i];
            var color = baseColor;
            if(depth >= 0){
                color = colorsList[depth][i];
            }
            pixels[i] = color;
        }
        return pixels;
    }
    
}
